import { $t } from "../../plugins/i18n";
import { VmErr } from "../svm/VmErr";
import { DataBus } from "../utils/DataBus";
import { BaseNode } from "./com/BaseNode";
export class ExcuteShellNode extends BaseNode {
    public value: string = ''
    constructor(name?: string, val?: string) {
        super(name ? name : 'ExcuteShell')
        this.edit.out = false
        this.type = 'ExcuteShell'
        this.canReference = true
        this.bgColor = '#808080'
        this.addInput({ key: 'line' })
        this.addInput({ key: 'input' })
        this.addOutput({ key: 'output' })

        this.addAttribute({
            name: "reference",
            value: val ? val : 'none',
            type: 'string',
            options: undefined,
            disable: true
        })

        this.onAttributeClick = this.onAttributeClickHandler.bind(this)
        this.refresh()
    }
    addAttribute(attr: NodeAttribute) {
        super.addAttribute(attr)
    }

    verify(): boolean {
        const connect = this.getAttribute('reference')
        const input = this.getInput('input')
        if (!connect?.value && !input?.socket?.connection) {
            throw new VmErr(this.id, this.titleContent + ' input or connect undefined')
        }
        return true
    }
    onAttributeClickHandler(attr: NodeAttribute) {
        if (attr.name !== 'reference') {
            return
        }
        const line = this.getInput('line')?.socket?.connection?.node
        if (line) {
            const parent = DataBus.nodes.find(node => node.id == line)
            if (!parent || parent.type !== 'Function') {
                DataBus.nodesSelector.show((node => {
                    attr.value = node.nodeId
                    this.createAttributes()
                    DataBus.nodesSelector.hide()
                }),undefined,this)
                return
            }
            const args = parent.getAttribute('argsNumber')?.value
            if (!args) {
                throw new VmErr(this.id, 'args error')
            }
            const argsNumber = parseInt(args)
            const ar: SelectBtnInfo[] = []
            for (let i = 0; i < argsNumber; i++) {
                ar.push({
                    nodeId: `${parent.id}_args_${i}`,
                    title: `args_${i}`
                })
            }
            DataBus.nodes.forEach(node => {
                if (parent.id != node.id && node.canReference) {
                    ar.push({
                        nodeId: node.id,
                        title: node.titleContent
                    })
                }
            })
            DataBus.nodesSelector.show((node => {
                attr.value = node.nodeId
                console.log($t(attr.value))
                this.createAttributes()
                DataBus.nodesSelector.hide()
            }), ar,this)
        } else {
            DataBus.nodesSelector.show((node => {
                attr.value = node.nodeId
                this.createAttributes()
                DataBus.nodesSelector.hide()
            }),undefined,this)
        }

    }
}